//
//  NewsViewController.m
//  网易
//
//  Created by Roach on 16/6/22.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsModel.h"
#import "NewsTableViewCell.h"
#import "GatHeightTools.h"
#import "ImageTableViewCell.h"


@interface NewsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *newsModelArray;



@end

@implementation NewsViewController

static NSString *const newsCellId = @"newsCell";
static NSString *const imageCellId = @"imageCell";

- (void)viewDidLoad {
    
    [self createTableView];
    [self config];
    [self values];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)config {
    
    self.newsModelArray = [[NSMutableArray alloc]init];
    
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sousuo.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"zhibo.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
    self.navigationItem.title = @"新闻";
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.titleTextAttributes = @{
                                                                    
                                                                    NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:27]
                                                                    
                                                                    };
    
}

- (void)rightAction:(UIBarButtonItem *)rightButton {
    
    NSLog(@"右按钮");
    
}

- (void)leftAction:(UIBarButtonItem *)leftButton {
    
    NSLog(@"左按钮");
    
}

- (void)values {
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"duanzi" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *duanziArray = [dictionary objectForKey:@"段子"];
    
    for (NSDictionary *dic in duanziArray) {
        
        NewsModel *newsModel = [[NewsModel alloc]init];
        [newsModel setValuesForKeysWithDictionary:dic];
        [self.newsModelArray addObject:newsModel];
        
        [newsModel release];
        
    }
    
}

- (void)createTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    
    
    [self.view addSubview:_tableView];
    [_tableView release];
    
    [_tableView registerClass:[NewsTableViewCell class] forCellReuseIdentifier:newsCellId];
    
    [_tableView registerClass:[ImageTableViewCell class] forCellReuseIdentifier:imageCellId];
    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.newsModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    if ([[self.newsModelArray objectAtIndex:indexPath.row] img]) {
        
        NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsCellId];
        
        NewsModel *newsModel = [self.newsModelArray objectAtIndex:indexPath.row];
        
        cell.newsCellModel = newsModel;
        
        return cell;
        
    }else {
        
        ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imageCellId];
        
        NewsModel *newsModel = [self.newsModelArray objectAtIndex:indexPath.row];
        
        cell.imageModel = newsModel;
        
        return cell;
        
        
    }
    
    
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    if (indexPath.row % 2) {
    
        NewsModel *newsModel = self.newsModelArray[indexPath.row];
        return [GatHeightTools heightWith:newsModel.digest] + 20;
        
//    }else {
    
        
        
//    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
